package com.github.glideplus.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.github.glideplus.ImageLoader;
import com.github.glideplus.impl.DisplayOptions;
import com.github.glideplus.impl.GlideLoader;
import com.github.glideplus.transform.CustomShapeTransformation;
import com.github.glideplus.view.SimpleProgressTarget;

import java.io.IOException;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropTransformation;
import jp.wasabeef.glide.transformations.MaskTransformation;


/**
 * Created by zoudong on 2017/2/18.
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder> {

    Context context;

    public ImageAdapter(Context context) {
        this.context = context;
    }

    String[] ss = {
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487418421432&di=e36eecb8bc06d6776cd73859da538569&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fa2cc7cd98d1001e9460fd63bbd0e7bec54e797d7.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487423598830&di=5908c77368a2f022f59b8b0388c062e7&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F5882b2b7d0a20cf482c772bf73094b36acaf997f.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487418421947&di=2bd56e025f329747be29dabd0a3b1fcb&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F7a899e510fb30f24a9a92e37cd95d143ad4b03d7.jpg"};
    private static final String IMAGE_1_SMALL =
            "https://images.unsplash.com/photo-1452475168325-672188dc4dc3?h=600&w=600&fit=crop";
    private static final String IMAGE_2_LARGE =
            "https://images.unsplash.com/photo-1448518184296-a22facb4446f?h=1200&w=1200&fit=crop";

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_img_layout, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String s = ss[position % 3];
//        Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new MaskTransformation(context,R.mipmap.ic_launcher))
//                    .into(holder.progressTarget);
        if (position == 0) {
            Glide.with(context).load(IMAGE_2_LARGE).diskCacheStrategy(DiskCacheStrategy.NONE).placeholder(R.mipmap.ic_launcher).thumbnail(Glide.with(context).load(IMAGE_1_SMALL)
            )
                    .into(holder.imageView);
//            ImageLoader.getInstance().displayImage(context, s, holder.imageView);
            holder.textView.setText("普通");
        } else if (position == 1) {
            ImageLoader.getInstance().displayCircleImage(context, s, holder.imageView);
            holder.textView.setText("圆形");
        } else if (position == 2) {
            ImageLoader.getInstance().displayRoundImage(context, ss[1], 20, 0, holder.imageView, DisplayOptions.RoundModel.CornerType.BOTTOM);
            holder.textView.setText("圆角 下");
        } else if (position == 3) {
            holder.textView.setText("圆角  上");
            ImageLoader.getInstance().displayRoundImage(context, ss[1], 20, 0, holder.imageView, DisplayOptions.RoundModel.CornerType.TOP);
        } else if (position == 4) {
            holder.textView.setText("圆角  右");
            ImageLoader.getInstance().displayRoundImage(context, ss[1], 20, 0, holder.imageView, DisplayOptions.RoundModel.CornerType.RIGHT);
        } else if (position == 5) {
            holder.textView.setText("圆角  左   + margin");
            ImageLoader.getInstance().displayRoundImage(context, ss[1], 20, 20, holder.imageView, DisplayOptions.RoundModel.CornerType.BOTTOM);
        } else if (position == 6) {
            holder.textView.setText("图片缩放");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new BlurTransformation(context, 10, 2))
                    .into(holder.imageView);
        } else if (position == 7) {
            holder.textView.setText("new CropTransformation(context)");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new CropTransformation(context))
                    .into(holder.imageView);
        } else if (position == 8) {
            holder.textView.setText("new ColorFilterTransformation(context)");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher)
                    .bitmapTransform(new ColorFilterTransformation(context, Color.parseColor("#d9ff0000")))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(holder.imageView);
        } else if (position == 9) {//高斯模糊
            holder.textView.setText("高斯模糊");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new BlurTransformation(context, 10, 2))
                    .into(holder.imageView);
        } else if (position == 10) {
            holder.textView.setText("ColorFilterTransformation  颜色");
            Glide.with(context).load(s).bitmapTransform(new ColorFilterTransformation(context, Color.parseColor("#d9000000")))
                    .diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.imageView);
        } else if (position == 11) {
            holder.textView.setText("CustomShapeTransformation");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new CustomShapeTransformation(context, R.mipmap.ic_launcher),new CenterCrop(context))
                    .diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.imageView);
        } else if (position == 12) {
            holder.textView.setText("CustomShapeTransformation PorterDuff.Mode.DST_OVER");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new CustomShapeTransformation(context, R.mipmap.ic_launcher, PorterDuff.Mode.DST_OVER),new CenterCrop(context))
                    .diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.imageView);
        } else if (position == 13) {
            holder.textView.setText("MaskTransformation PorterDuff.Mode.DST_OVER");
            Glide.with(context).load(s).placeholder(R.mipmap.ic_launcher).bitmapTransform(new MaskTransformation(context, R.mipmap.ic_launcher),new CenterCrop(context))
                    .diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.imageView);
        } else if (position == 14) {
            holder.textView.setText("BitmapCrossfadeListener");
            Glide.with(context)
                    .load(s)
                    .asBitmap()
                    .centerCrop().diskCacheStrategy(DiskCacheStrategy.NONE)
                    .dontAnimate() // turn off defaults since we're handling it
                    .listener(new BitmapCrossfadeListener<String>() {
                    })
                    .placeholder(R.mipmap.ic_launcher)
                    .into(holder.imageView);
        } else if (position == 15){
            Glide.with(context)
                    .load(R.drawable.load)
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .dontAnimate() // turn off defaults since we're handling it
                    .placeholder(R.mipmap.ic_launcher)
                    .into(holder.imageView);
//            holder.imageView.setImageBitmap(GlideLoader.transformDrawable(context, context.getResources().getDrawable(R.drawable.load), new CropCircleTransformation(context), 400, 400).getBitmap());
        }

    }

    @Override
    public int getItemCount() {
        return 16;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView textView;
        public ProgressBar progressBar;
        public SimpleProgressTarget<GlideDrawable> progressTarget;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.img_item);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
            textView = (TextView) itemView.findViewById(R.id.tv_name);
            progressTarget = new SimpleProgressTarget<>(new ImageViewTarget<GlideDrawable>(imageView) {
                @Override
                protected void setResource(GlideDrawable resource) {
                    view.setImageDrawable(resource);
                }
            }, progressBar, imageView, null);
        }
    }
}
